#ifndef FILESYSTEM_H
#define FILESYSTEM_H

#include "Common.h"
#include "File.h"
#include "FileInfo.h"

class FileSystem{
private:
    string _localDirectory;
    vector<File*> _files;
    int _pointer;
    pthread_mutex_t files_mutex;
    pthread_mutex_t file_writing_mutex;

    string convertInt(int number);
    int findFileIndex(string fileName, int & index);
    int addFile(string fileName, int versionNum);
    int addVersion(string fileName, int versionNum, Host * host, int timestamp);

public:
    FileSystem();
    ~FileSystem();

    // file system basic operations 
    void list();
    int modify(string fileName, int versionNum);
    int insert(string fileName, int timeStamp);
    int save(string fileName, int versionNum, int timeStamp);
    int tag(string fileName, int versionNum, int timeStamp);

    // stuff to do for peer
    int getHostedFiles(string &message);
    bool isUpToDate(string fileName, int versionNum);
    int getPhysicalFile(string fileName, int versionNum, vector<string> &chunks, int &timeStamp );
    int getFileSystem(string ip, int port, string &message);
    vector<FileInfo *> SyncFileSystem(char * message);
    vector<FileInfo *> filesToBroadcast();
    int removeHost(string ipadd, int port);
    bool isModified(string fileName, int versionNum);
    File::Status status(string fileName, int versionNum);
    bool hasLocalCopy(string fileName, int versionNum);
    bool exist(string fileName, int versionNum);
    Host * getHost(string fileName, int versionNum);
    string getShortName(string longName);
    void setVersionInTransit(string fileName, int versionNum);
    bool isVersionInTransit(string fileName, int versionNum);
    void removeAllHosts();
    Host * getNextFileToPull(string &fileName, int &versionNum);

    // disk operations
    int writeToDisk(string fileName, int versionNum, int chunkNum, char * content, int responseLen);
    int combineFileOnDisk(string fileName, int versionNum, int totalChunks);

    // some file operations in the background 
    int updateNetworkTimestamp(string fileName, int versionNum, Host * host, int timestamp);
    int receivedFile(string fileName, int versionNum, int timestamp);
    int deleteFile(string fileName, int timestamp); 

};

#endif
